package com.ac.common.core.security;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.exception.config.ExceptionType;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author : acfan
 * date : create in 2023/7/19 12:48
 * description :
 **/

public class Sha {
    public static String sha1(String str) throws BaseException {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            byte[] encodedHash = digest.digest(str.getBytes(StandardCharsets.UTF_8));
            StringBuilder hexString = new StringBuilder();
            for (byte b : encodedHash) {
                String hex = String.format("%02x", b);
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new BaseException(ExceptionType.System_Error, e);
        }

    }
}
